package com.lintf.wxpay.task;

import com.lintf.business.entity.OrderInfo;
import com.lintf.wxpay.service.OrderInfoService;
import com.lintf.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class WxPayTask {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private WxPayService wxPayService;

    /**
     * 从第0秒开始每隔30s执行1次，查询创建超过5分钟并且未支付的订单
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void orderConfirm() {
        log.info("NativeQuery定时任务执行：开始");
        List<OrderInfo> orderInfoList = orderInfoService.getNoPayOrderByDuration(5);
        orderInfoList.forEach(item -> {
            String orderNo = item.getOrderNo();
            log.info("NativeQuery定时任务执行：超时未支付订单号：{}",orderNo);

            //TODO 核实订单状态，调用微信查询订单API
            try {
                wxPayService.checkOrder(orderNo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        log.info("NativeQuery定时任务执行：完成");
    }

}
